<?php
class AdminAction extends Action
{
    public function index()
    {
        if (!$this->check())
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
        else if (!session('power') == 1)
            redirect(__ROOT__ . '/Index', 0, ' 登录成功！自动转跳中~	');
        
        $title = "未出厂飞机信息";
        
        $fly = M('fly_info');
        import('ORG.Util.Page'); // 导入分页类
        
        if (isset($_GET['context'])) {
            $sql = "no='" . $_GET['context'] . "' OR man='" . $_GET['context'] . "' AND state=1";
        } else {
            $sql = "state=1";
        }
        
        $count = $fly->where($sql)->count();
        $Page  = new Page($count, 10);
        $show  = $Page->show($sql);
        if ($count == 0 && $sql=="state=1") {
            $tips[0] = "数据库没有任何记录！";
			$tips[1] = "请添加记录~";
        }else if($count == 0){
			$tips[0] = "没有相关记录！";
			$tips[1] = "";
		}else{
			$tips[0] = "";
			$tips[1] = "";
		}
        
        $data = $fly->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $unit = M('unit');
        for ($i = 0; $i < count($data); $i++) {
            $name             = $unit->where('id=%d', $data[$i]['unit'])->find();
            $data[$i]['unit'] = $name['name'];
        }
        
        $this->assign("fly", $data);
        $this->assign('page', $show); //赋值分页输出
        $this->assign("title", $title);
        $this->assign("tips", $tips);
        $this->assign("search", "");
        $this->display();
    }
    
    
    public function in()
    {
        $title = "飞机入厂登记";
        $this->assign('title', $title);
        $this->assign("search", "disabled");
        if ($this->check() && session('power') == 1) {
            if (isset($_POST['unit']) && isset($_POST['no']) && isset($_POST['revi']) && isset($_POST['man']) && isset($_POST['date'])) {
                $flag          = true;
                $data['unit']  = $_POST['unit'];
                $data['no']    = $_POST['no'];
                $data['man']   = $_POST['man'];
                $data['in']    = $_POST['date'];
                $data['revi']  = $_POST['revi'];
                $data['state'] = 1;
                
                $regexno   = '/^\d{4}$/';
                $regexdate = '/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/';
                $regexman  = '/^\d{7}$/';
                $matches   = array();
                
                
                if (!is_numeric($data['unit']))
                    $flag = false;
                else if (!is_numeric($data['revi']))
                    $flag = false;
                else if (!preg_match($regexno, $data['no'], $matches))
                    $flag = false;
                else if (!preg_match($regexdate, $data['in'], $matches))
                    $flag = false;
                else if (!preg_match($regexman, $data['man'], $matches))
                    $flag = false;
                
                if ($flag) {
                    $fly    = M("fly_info");
                    $result = $fly->where("no='%s' and state=1", $data['no'])->find();
                    if ($result == false || $result == null) {
                        if ($fly->add($data)) {
                            echo "<script>alert('记录添加成功~');</script>";
                        }
                    } else {
                        echo "<script>alert('飞机已在厂！');</script>";
                    }
                } else {
                    echo "<script>alert('有错误输入，请重新输入！');</script>";
                }
            }
            $unit = M("unit");
            $data = $unit->select();
            $this->assign("data", $data);
            $this->display();
        } else
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
    }
    
    public function out()
    {
        $title = "飞机出厂登记";
        $this->assign('title', $title);
        $this->assign("search", "disabled");
        if ($this->check() && session('power') == 1) {
            if (isset($_GET['context'])) {
                $regexno = '/^\d{4}$/';
                if (preg_match($regexno, $_GET['context'], $matches)) {
                    $readonly = "readonly='readonly'";
                    $no       = $_GET['context'];
                } else {
                    $readonly = "";
                    $no       = "";
                }
            }
            
            if (isset($_POST['no']) && isset($_POST['date'])) {
                $flag          = true;
                $data['no']    = $_POST['no'];
                $data['out']   = $_POST['date'];
                $data['state'] = 0;
                
                
                
                $regexno   = '/^\d{4}$/';
                $regexdate = '/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/';
                
                if (!preg_match($regexno, $data['no'], $matches))
                    $flag = false;
                else if (!preg_match($regexdate, $data['out'], $matches))
                    $flag = false;
                
                
                if ($flag) {
                    $fly    = M("fly_info");
					$photo = M("photos_info");
                    $result = $fly->where("no='%s' and state=1", $data['no'])->find();
                    if ($result != false && $result != null) {
                        if ($fly->where("no='%s' and state=1", $data['no'])->save($data) && $photo->where("plane='%s'",$data['no'])->delete()) {
                            echo "<script>alert('操作成功~');</script>";
                        }
                    } else {
                        echo "<script>alert('没有" . $data['no'] . "的入厂记录');</script>";
                    }
                } else {
                    echo "<script>alert('有错误输入，请重新输入！');</script>";
                }
            }

            $this->assign("no", $no);
            $this->assign("readonly", $readonly);
            $this->display();
        } else
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
    }
    
    public function add()
    {
        $title = "照片添加";
        
        $search = "disabled";
        
        $styles = array(
            "button" => array(
                "active",
                ""
            ),
            "box" => array(
                "",
                "display:none"
            ),
            "tips" => array(
                "display:none",
                "display:none"
            )
        );
        $Tdata  = null;
        $tips   = null;
        
        
        if ((isset($_GET['ch']) && $_GET['ch'] == 'modi') || isset($_GET['context'])) {
            $title    = "照片修改";
            $search   = "";
            $matches  = array();
            $regexman = '/^\d{7}$/';
            $regexno  = '/^\d{4}$/';
            if (isset($_GET['context'])) {
                if (preg_match($regexman, $_GET['context'], $matches)) {
                    $fly = M("fly_info");
                    $res = $fly->where("man='%s' and state=1", $_GET['context'])->find();
                    if ($res) {
                        $res = M("photos_info")->where("plane='%s'", $res['no'])->select();
                        if ($res) {
                            $Tdata  = $res;
                            $styles = array(
                                "button" => array(
                                    "",
                                    "active"
                                ),
                                "box" => array(
                                    "display:none",
                                    ""
                                ),
                                "tips" => array(
                                    "display:none",
                                    "display:none"
                                )
                            );
                        } else {
                            $styles = array(
                                "button" => array(
                                    "",
                                    "active"
                                ),
                                "box" => array(
                                    "display:none",
                                    ""
                                ),
                                "tips" => array(
                                    "display:none",
                                    ""
                                )
                            );
                            $tips   = "没有相关记录！";
                        }
                    } else {
                        $styles = array(
                            "button" => array(
                                "",
                                "active"
                            ),
                            "box" => array(
                                "display:none",
                                ""
                            ),
                            "tips" => array(
                                "display:none",
                                ""
                            )
                        );
                        $tips   = "没有相关记录！";
                    }
                } else if (preg_match($regexno, $_GET['context'], $matches)) {
                    $res = M("photos_info")->where("plane='%s'", $_GET['context'])->select();
                    if ($res) {
                        $Tdata  = $res;
                        $styles = array(
                            "button" => array(
                                "",
                                "active"
                            ),
                            "box" => array(
                                "display:none",
                                ""
                            ),
                            "tips" => array(
                                "display:none",
                                "display:none"
                            )
                        );
                    } else {
                        $styles = array(
                            "button" => array(
                                "",
                                "active"
                            ),
                            "box" => array(
                                "display:none",
                                ""
                            ),
                            "tips" => array(
                                "display:none",
                                ""
                            )
                        );
                        $tips   = "没有相关记录！";
                    }
                } else if (is_numeric($_GET['context'])) {
                    $res = M("photos_info")->where("id=%d", $_GET['context'])->select();
                    if ($res) {
                        $Tdata  = $res;
                        $styles = array(
                            "button" => array(
                                "",
                                "active"
                            ),
                            "box" => array(
                                "display:none",
                                ""
                            ),
                            "tips" => array(
                                "display:none",
                                "display:none"
                            )
                        );
                    } else {
                        $styles = array(
                            "button" => array(
                                "",
                                "active"
                            ),
                            "box" => array(
                                "display:none",
                                ""
                            ),
                            "tips" => array(
                                "display:none",
                                ""
                            )
                        );
                        $tips   = "没有相关记录！";
                    }
                } else {
                    $styles = array(
                        "button" => array(
                            "",
                            "active"
                        ),
                        "box" => array(
                            "display:none",
                            ""
                        ),
                        "tips" => array(
                            "display:none",
                            ""
                        )
                    );
                    $tips   = "没有相关记录！";
                }
                
            } else {
                $styles = array(
                    "button" => array(
                        "",
                        "active"
                    ),
                    "box" => array(
                        "display:none",
                        "display:none"
                    ),
                    "tips" => array(
                        "",
                        "display:none"
                    )
                );
            }
        } else {
            $styles = array(
                "button" => array(
                    "active",
                    ""
                ),
                "box" => array(
                    "",
                    "display:none"
                ),
                "tips" => array(
                    "display:none",
                    "display:none"
                )
            );
        }
        
        if ($this->check() && session('power') == 1) {
            if (isset($_POST['no']) && isset($_POST['major']) && isset($_POST['place']) && isset($_POST['wtime'])
            //isset($_POST['wdate']) && isset($_POST['pdate']) && isset($_POST['photor']) && isset($_POST['url']) &&
            /*isset($_POST['checker']) && isset($_POST['poster'])*/ ) {
                $data = array();
                $flag = true;
                $name = $_POST['no'] . '-' . $_POST['pdate'] . '-' . $_POST['place'];
                $file = explode('.', $_POST['url']);
                $file = $file[sizeof($file) - 1];
                $dir  = getcwd();
                $url  = $dir . '/Public/Photos/' . $name . '.' . $file;
                if (file_exists($dir . '/Public/Photos/tmp/' . $_POST['url'])) {
                    if (rename($dir . '/Public/Photos/tmp/' . $_POST['url'], $url)) {
                        $url = __ROOT__ . '/Public/Photos/' . $name . '.' . $file;
                    } else
                        $flag = false;
                } else {
                    $flag = false;
                }
                
                if (flag) {
                    $data['plane']   = $_POST['no'];
                    $data['major']   = $_POST['major'];
                    $data['place']   = $_POST['place'];
                    $data['wtime']   = $_POST['wtime'];
                    $data['wdate']   = $_POST['wdate'];
                    $data['pdate']   = $_POST['pdate'];
                    $data['photor']  = $_POST['photor'];
                    $data['url']     = $url;
                    $data['checker'] = $_POST['checker'];
                    $data['poster']  = $_POST['poster'];
                    $photo           = M("photos_info");
                    $result          = $photo->where("plane='%s' and place=%d", $data['plane'], $data['place'])->find();
                    
                    //判断是否已经有记录
                    if ($result == null or $result == false) {
                        if (isset($_GET['ch']) && $_GET['ch'] == 'modi') {
                            if ($photo->where("plane='%s'", $data['plane'])->save($data)) {
                                echo "<script>alert('记录修改成功~');</script>";
                                echo "<script>window.history.go(0);</script>";
                            } else {
                                echo "<script>alert('记录修改失败~');</script>";
                                echo "<script>window.history.go(-1);</script>";
                            }
                        } else {
                            if ($photo->add($data)) {
                                echo "<script>alert('记录添加成功~');</script>";
                                echo "<script>window.history.go(0);</script>";
                            } else {
                                echo "<script>alert('记录添加失败！');</script>";
                                echo "<script>window.history.go(-1);</script>";
                            }
                        }
                    } else {
                        echo "<script>alert('该部件记录已经添加。');</script>";
                        echo "<script>window.history.go(-1);</script>";
                    }
                } else {
                    echo "<script>alert('未知错误！');</script>";
                    echo "<script>window.history.go(-1);</script>";
                }
            }
            
            
            $fly       = M("fly_info");
            $datas_fly = $fly->where("state=1")->select();
            $this->assign("fly", $datas_fly);
            
            $photos       = M("photos_position");
            $datas_photos = $photos->select();
            $this->assign("photos", $datas_photos);
            
            $majors       = M("major");
            $datas_majors = $majors->select();
            $this->assign("majors", $datas_majors);
            
            $wtimes       = M("wtime");
            $datas_wtimes = $wtimes->select();
            $this->assign("wtime", $datas_wtimes);
            
            $this->assign("styles", $styles);
            $this->assign("tips", $tips);
            $this->assign("Tdata", $Tdata);
            
            $this->assign('title', $title);
            $this->assign("search", $search);
            $this->display();
        } else
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
    }
    
    
    public function modi_photo()
    {
        $title = "信息修改";
        $this->assign("search", "disabled");
        $this->assign('title', $title);
        if ($this->check() && session('power') == 1) {
            $photos = M("photos_info");
            $datas  = $photos->select();
            $fly    = M("fly_info");
            for ($i = 0; $i < count($datas); $i++) {
                $data             = $fly->where("no='%s'", $datas[$i]['plane'])->find();
                $datas[$i]['man'] = $data['man'];
            }
            
            $this->assign("photo", $datas);
            
            $fly   = M("fly_info");
            $datas = $fly->select();
            $this->assign("fly", $datas);
            
            $photos = M("photos_position");
            $datas  = $photos->select();
            $this->assign("photos", $datas);
            
            $majors = M("major");
            $datas  = $majors->select();
            $this->assign("majors", $datas);
            
            $wtimes = M("wtime");
            $datas  = $wtimes->select();
            $this->assign("wtime", $datas);
            
            $this->display();
        } else
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
    }
    
    public function modi_plane()
    {
        $title = "修改飞机信息";
        $this->assign("search", "disabled");
        $style  = "display:none";
        $txtBox = "display:none";
        $this->assign('title', $title);
        $data = array();
        $flag = 1;
        if ($this->check() && session('power') == 1) {
            if (isset($_GET['context'])) {
                if (is_numeric($_GET['context'])) {
                    $db  = M('fly_info');
                    $res = $db->where("(no='%s' or man='%s') and state=1", $_GET['context'], $_GET['context'])->select();
                    if ($res == null or $res == false)
                        $flag = 3; //没有记录
                    else {
                        $data = $res;
                        $flag = 4;
                    }
                } else {
                    $flag = 2; //无效输入	
                }
            } else {
                $txtBox = "";
            }
            if ($flag == 2) {
                $d['tips'] = "无效输入！";
                
                $this->assign("tips", $d);
            }
            if ($flag == 3) {
                $d['tips'] = "没有相关记录！";
                $this->assign("tips", $d);
            }
            if ($flag == 4) {
                //配置单位的输出
                $d = M('unit')->select();
                for ($i = 0; $i < count($d); $i++) {
                    if ($d[$i]['id'] == $data[0]['unit'])
                        $d[$i]['state'] = "selected";
                    else
                        $d[$i]['state'] = "";
                }
                $this->assign("unit", $d);
                $d               = M('unit')->where('id=%d', $data[0]['unit'])->find();
                $data[0]['unit'] = $data[0]['unit'] . "." . $d['name'];
                
                //维修次数的输出
                $d = array();
                for ($i = 0; $i < 3; $i++) {
                    if ($i + 1 == $data[0]['revi']) {
                        $d[$i]['state'] = "selected";
                    } else {
                        $d[$i]['state'] = "";
                    }
                    $d[$i]['id'] = $i + 1;
                }
                $this->assign("revi", $d);
                
                $style = "";
                $this->assign("data", $data);
            }
            
            $this->assign("style", $style);
            $this->assign("txtBox", $txtBox);
            $this->display();
        }
    }
    
    public function mana()
    {
        if ($this->check() && session('power') == 1) {
            $title = "信息管理";
            $this->assign("search", "disabled");
            $this->assign("title", $title);
            $data = $this->getMana();
            
            $datas   = array(
                "unit",
                "major",
                "photos_position",
                "wtime"
            );
            $counter = array(
                0 => "active",
                1 => " ",
                2 => " ",
                3 => " "
            );
            
            if (isset($_GET['ch'])) {
                for ($i = 0; $i < count($datas); $i++) {
                    if ($_GET['ch'] == $datas[$i]) {
                        $counter[$i] = "active";
                    } else {
                        $counter[$i] = " ";
                    }
                }
            }
            
            $this->assign("data", $data);
            $this->assign("style", "");
            $this->assign("active", $counter);
            
            $this->display();
        } else
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
    }
    
    public function getMan()
    {
        if (isset($_POST['no'])) {
            $fly  = M("fly_info");
            $data = $fly->where("no='%s'", $_POST['no'])->find();
            if ($data != null && $data != false) {
                echo $data['man'];
            } else
                echo 'error';
        } else
            echo 'error';
    }
    public function addImg()
    {
        //导入图片上传类  
        import("ORG.Net.UploadFile");
        //实例化上传类  
        $upload            = new UploadFile();
        $upload->maxSize   = 3145728;
        //设置文件上传类型  
        $upload->allowExts = array(
            'jpg',
            'gif',
            'png',
            'jpeg'
        );
        //设置文件上传位置  
        $upload->savePath  = "./Public/Photos/tmp/"; //上传的临时文件 
        //设置文件上传名(按照时间)  
        $upload->saveRule  = "time";
        if (!$upload->upload()) {
            echo "error";
        } else {
            //上传成功，获取上传信息
            $data = $upload->getUploadFileInfo();
            echo $data[0]['savename'];
        }
        
    }
    
    public function dataChange()
    {
        if (isset($_GET['datas']) && isset($_GET['id']) && isset($_GET['data'])) {
            $db = M($_GET['data']);
            if ($db != null) {
                $res = $db->where("name='%s'", $_GET['datas'])->find();
                if ($res != false or $res != null) {
                    echo 2;
                    return;
                }
                $data['name'] = $_GET['datas'];
                $res          = $db->where("id=%d", $_GET['id'])->save($data);
                if (!$res) {
                    echo 3;
                    return;
                }
                echo 0; //成功
            } else {
                echo 1;
                return;
            }
        } else {
            echo 1;
            return;
        }
    }
    
    public function dataDel()
    {
        if (isset($_GET['id']) && isset($_GET['data'])) {
            $data = M($_GET['data']);
            if ($data == null or $data == false) {
                echo 1;
                return;
            }
            $res = $data->where("id=%d", $_GET['id'])->delete();
            if ($res != false) {
                echo 0;
                return;
            }
        } else
            echo 1;
    }
    
    public function dataAdd()
    {
        if (isset($_GET['datas']) && isset($_GET['data']) && isset($_GET['id'])) {
            $data = M($_GET['data']);
            if ($data == null or $data == false) {
                echo 2;
                return;
            }
            
            $res = $data->where("name='%s'", $_GET['datas'])->select();
            
            if ($res != null or $res != false) {
                echo 3;
                return;
            }
            
            $res = $data->where("id=%d", $_GET['id'])->select();
            
            if ($res != null or $res != false) {
                echo 4;
                return;
            }
            
            $Tdata['id']   = $_GET['id'];
            $Tdata['name'] = $_GET['datas'];
            $res           = $data->add($Tdata);
            if ($res) {
                echo 1;
                return;
            } else {
                echo 2;
            }
        }
    }
    
    public function changePlane()
    {
        if (isset($_POST['unit']) && isset($_POST['no']) && isset($_POST['revi']) && isset($_POST['man']) && isset($_POST['in']) && $this->check() && session('power') == 1) {
            $flag         = true;
            $data['unit'] = $_POST['unit'];
            $data['no']   = $_POST['no'];
            $data['man']  = $_POST['man'];
            $data['in']   = $_POST['in'];
            $data['revi'] = $_POST['revi'];
            
            $regexno   = '/^\d{4}$/';
            $regexdate = '/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/';
            $regexman  = '/^\d{7}$/';
            $matches   = array();
            
            
            if (!is_numeric($data['unit']))
                $flag = false;
            else if (!is_numeric($data['revi']))
                $flag = false;
            else if (!preg_match($regexno, $data['no'], $matches))
                $flag = false;
            else if (!preg_match($regexdate, $data['in'], $matches))
                $flag = false;
            else if (!preg_match($regexman, $data['man'], $matches))
                $flag = false;
            
            if ($flag) {
                $fly = M("fly_info");
                if ($fly) {
                    $res = $fly->where("no='%s' and state=1", $data['no'])->save($data);
                    if ($res) {
                        redirect(__ROOT__ . '/Admin/Index', 1, ' <script>alert("修改成功~")</script>');
                    } else {
                        redirect(__ROOT__ . '/Admin/Index', 1, ' <script>alert("修改成功~")</script>');
                    }
                } else {
                    echo "<script>alert('数据库不能打开~');</script>";
                }
            } else {
                echo "<script>alert('格式错误~');</script>";
            }
        } else {
            echo "<script>alert('非法操作~');</script>";
        }
        echo "<script>window.history.back(-1);</script>";
    }
    
    public function delPlane()
    {
        if ($this->check() && session('power') == 1) {
            if (isset($_GET['no'])) {
                $fly = M("fly_info");
				$photo = M("photos_info");
                if ($fly->where("no='%s'", $_GET['no'])->delete() && $photo->where("plane='%s'", $_GET['no'])->delete()) {
                    echo 0;
                } else {
                    echo 1;
                }
            } else {
                echo 1;
            }
        } else
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
    }
    
    public function changePhoto()
    {
        if ($this->check() && session('power') == 1) {
            if (isset($_POST['id']) && isset($_POST['no']) && isset($_POST['major']) && isset($_POST['place']) && isset($_POST['wtime'])
            //isset($_POST['wdate']) && isset($_POST['pdate']) && isset($_POST['photor']) && isset($_POST['url']) &&
            /*isset($_POST['checker']) && isset($_POST['poster'])*/ ) {
                $data = array();
                $flag = true;
                $name = $_POST['no'] . '-' . $_POST['pdate'] . '-' . $_POST['place'];
                $file = explode('.', $_POST['url']);
                $file = $file[sizeof($file) - 1];
                $dir  = getcwd();
                $url  = $dir . '/Public/Photos/' . $name . '.' . $file;
                
                $regexno   = '/^\d{4}$/';
                $regexdate = '/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/';
                $regexman  = '/^\d{7}$/';
                $matches   = array();
                
                $photo = M("photos_info")->where("id=%d", $_POST['id'])->find();
                
                if ($_POST['url'] == $photo['url']) {
                    if (__ROOT__ . '/Public/Photos/' . $name . '.' . $file != $photo['url']) {
                        if (rename(realpath("..") . $_POST['url'], $url)) {
                            $flag = true;
                        } else {
                            $flag = false;
                        }
                        $url = __ROOT__ . '/Public/Photos/' . $name . '.' . $file;
                    } else {
                        $url  = $_POST['url'];
                        $flag = true;
                    }
                } else if (file_exists($dir . '/Public/Photos/tmp/' . $_POST['url'])) {
                    if (file_exists($dir . '/Public/Photos/' . $name . '.' . $file)) {
                        unlink($dir . '/Public/Photos/' . $name . '.' . $file);
                    }
                    
                    if (rename($dir . '/Public/Photos/tmp/' . $_POST['url'], $url)) {
                        $url  = __ROOT__ . '/Public/Photos/' . $name . '.' . $file;
                        $flag = true;
                    } else
                        $flag = false;
                } else {
                    $flag = false;
                }
                
                
                if ($flag) {
                    $data['plane']   = $_POST['no'];
                    $data['major']   = $_POST['major'];
                    $data['place']   = $_POST['place'];
                    $data['wtime']   = $_POST['wtime'];
                    $data['wdate']   = $_POST['wdate'];
                    $data['pdate']   = $_POST['pdate'];
                    $data['photor']  = $_POST['photor'];
                    $data['url']     = $url;
                    $data['checker'] = $_POST['checker'];
                    $data['poster']  = $_POST['poster'];
                    $photo           = M("photos_info");
                    $result          = $photo->where("plane='%s' and place=%d", $data['plane'], $data['place'])->find();
                    
                    $flag = true;
                    if ($result == null || $result == false) {
                        $flag = true;
                    } else if ($result['id'] != $_POST['id']) {
                        echo "<script>alert('该飞机此部位的拍照记录已经存在，请修改已经存在的记录。');</script>";
                        $flag = false;
                    } else if (!preg_match($regexdate, $data['wdate'], $matches)) {
                        $flag = false;
                        echo "<script>alert('工作日期格式有误，请检查~');</script>";
                    } else if (!preg_match($regexdate, $data['pdate'], $matches)) {
                        $flag = false;
                        echo "<script>alert('拍照日期格式有误，请检查~');</script>";
                    } else if ($data['photor'] == "" or $data['checker'] == "" or $data['poster'] == "") {
                        $flag = false;
                        echo "<script>alert('内容不能为空，请检查~');</script>";
                    }
                    if (!$flag)
                        echo "<script>window.history.back(-1);</script>";
                    else {
                        if ($photo->where("id=%d", $_POST['id'])->save($data)) {
                            redirect(__ROOT__ . '/Admin/view', 1, ' <script>alert("修改成功！")</script>');
                        } else {
                            redirect(__ROOT__ . '/Admin/view', 1, ' <script>alert("修改成功！")</script>');
                        }
                    }
                    
                } else {
                    echo "<script>alert('修改失败！');</script>";
                    echo "<script>window.history.back(-1);</script>";
                }
            } else {
                echo "<script>alert('非法操纵！');</script>";
                echo "<script>window.history.back(-1);</script>";
            }
        } else {
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
        }
        
    }
    
    public function delPhoto()
    {
        if ($this->check() && session('power') == 1) {
            if (isset($_GET['id'])) {
                $photo = M('photos_info');
                $url   = $photo->where("id=%d", $_GET['id'])->find();
                $url   = $url['url'];
                if ($photo->where("id=%d", $_GET['id'])->delete()) {
                    if (unlink(realpath("..") . $url))
                        echo 0;
                    else
                        echo 1;
                } else
                    echo 1;
            } else {
                echo 1;
            }
        } else {
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
        }
    }
    
    public function view()
    {
        $title = "照片浏览";
        $this->assign("search", "");
        
        $regexno  = '/^\d{4}$/';
        $regexman = '/^\d{7}$/';
        $matches  = array();
        $tips     = "";
        
        if ($this->check() && session('power') == 1) {
            $db = M("photos_info");
            import('ORG.Util.Page'); // 导入分页类
            
            //分页
            if (isset($_GET['context'])) {
                if (is_numeric($_GET['context'])) {
                    if (preg_match($regexman, $_GET['context'], $matches)) {
                        $db2 = M("fly_info");
                        $res = $db2->where("man = '%s'", $_GET['context'])->select();
                        
                        
                        if ($res) {
                            $count = 0;
                            for ($i = 0; $i < count($res); $i++) {
                                $count = $count + $db->where("plane='%s'", $res[$i]['no'])->count();
                            }
                            
                            //$count = $db->where()->count();
                            $Page = new Page($count, 12);
                            $show = $Page->show();
                            for ($i = 0; $i < count($res); $i++) {
                                $data = $db->where("plane='%s'", $res[$i]['no'])->select();
                                $data = array_slice($data, $Page->firstRow, $Page->listRows);
                            }
                        } else {
                            $count = $db->where()->count();
                            $Page  = new Page($count, 12);
                            $show  = $Page->show();
                            $data  = $db->where()->limit($Page->firstRow . ',' . $Page->listRows)->select();
                        }
                        if ($data == null || $data == false)
                            $tips = "没有该记录!";
                    } else if (preg_match($regexno, $_GET['context'], $matches)) {
                        $count = $db->where("plane='%s'", $_GET['context'])->count();
                        $Page  = new Page($count, 12);
                        $show  = $Page->show();
                        $data  = $db->where("plane='%s'", $_GET['context'])->limit($Page->firstRow . ',' . $Page->listRows)->select();
                        if ($data == null || $data == false)
                            $tips = "没有该记录!";
                    } else {
                        $count = $db->count();
                        $Page  = new Page($count, 12);
                        $show  = $Page->show();
                        $data  = $db->limit($Page->firstRow . ',' . $Page->listRows)->select();
                        if ($data == null || $data == false)
                            $tips = "输入格式不正确（将显示所有照片！）";
                    }
                } else {
                    $count = $db->count();
                    $Page  = new Page($count, 12);
                    $show  = $Page->show();
                    $data  = $db->limit($Page->firstRow . ',' . $Page->listRows)->select();
                    if ($data == null || $data == false)
                        $tips = "输入格式不正确（将显示所有照片！）";
                }
            } else {
                $count = $db->count();
                $Page  = new Page($count, 12);
                $show  = $Page->show();
                $data  = $db->limit($Page->firstRow . ',' . $Page->listRows)->select();
            }
            
            
            for ($i = 0; $i < count($data); $i++) {
                $name             = $data[$i]['url'];
                $temp             = explode("/", $name);
                $data[$i]['name'] = $temp[count($temp) - 1];
                
            }
            $this->assign("data", $data);
            $this->assign("title", $title);
            $this->assign('page', $show); // 赋值分页输出
            $this->assign('tips', $tips);
            $this->display();
        } else {
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
        }
        
    }
    
    private function getMana()
    {
        $datas = array(
            "unit",
            "major",
            "photos_position",
            "wtime"
        );
        
        if (isset($_GET['ch'])) {
            $txt = $_GET['ch'];
            
            $flag = false;
            
            foreach ($datas as $ch)
                if ($_GET['ch'] == $ch) {
                    $flag = true;
                    break;
                }
            if (!$flag) {
                $data  = M($datas[0]);
                $datas = $data->select();
                for ($i = 0; $i < count($datas); $i++) {
                    $datas[$i]['data'] = $txt;
                }
                return $datas;
            }
            
            
            $data  = M($txt);
            $datas = $data->select();
            for ($i = 0; $i < count($datas); $i++) {
                $datas[$i]['data'] = $txt;
            }
            return $datas;
        } else {
            $data  = M($datas[0]);
            $datas = $data->select();
            for ($i = 0; $i < count($datas); $i++) {
                $datas[$i]['data'] = $txt;
            }
            return $datas;
        }
        
    }
    
    //判断登陆是否过时
    private function check()
    {
        $cookie = cookie('token');
        $login  = M('user_login');
        $data   = $login->where('token="%s"', $cookie)->find();
        
        if ($cookie != null && $cookie != "" && $data != null && $data != false) {
            $id   = $data['id'];
            $date = $data['dietime'];
            
            if (strtotime($date) > time()) {
                $User = M("user");
                $user = $User->where("id = '%d'", $id)->find();
                session('username', $user['user']);
                session('power', $user['power']);
                return true;
            }
        }
        return false;
    }
	
	public function shift(){
		if(!$this->check() or session('power') != 1){
			redirect(__ROOT__ . '/Index', 1, ' <script>alert("非法操作！")</script>');
		}
		
		$datas = M("photos_info")->select();
		$dir = getcwd()."/Public/Photos";
		$this->del($dir,$datas);
		
		redirect(__ROOT__ . '/Admin/index', 1, ' <script>alert("操作成功！")</script>');
	}
	
	private function del($dir,$datas) {
  		$dh=opendir($dir);
		
  		while ($file=readdir($dh)) {
    		if($file!="." && $file!="..") {
				$flag = true;
				foreach($datas as $d){
					$name = explode("/",$d['url']);
					$name = $name[count($name)-1];
					
					if($name==$file){
						$flag = false;
						break;
					}
				}
      			$fullpath=$dir."/".$file;
      			if(!is_dir($fullpath) && $flag) {
          			unlink($fullpath);
      			} else if($flag) {
          			$this->del($fullpath,$data);
      			}
    		}
		}
  	}
}